package com.ruoyi.service.impl;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.constant.OrderConstant;
import com.ruoyi.domain.DriverCredit;
import com.ruoyi.domain.OrderInfo;
import com.ruoyi.domain.vo.OrderInfoVo;
import com.ruoyi.mapper.CancelMapper;
import com.ruoyi.mapper.CouponMapper;
import com.ruoyi.mapper.CreditMapper;
import com.ruoyi.service.CancelService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.Objects;
/**
 * @author : LeiMingkun
 * @date : 2024-10-26 10:03
 * @description : CancelServiceImpl
 **/
@Slf4j
@Service
public class CancelServiceImpl implements CancelService {

    @Resource
    private CancelMapper cancelMapper;
    @Resource
    private CreditMapper creditMapper;
    @Resource
    private CouponMapper couponMapper;
    @Resource
    private Redission
    /**
      * @Param: [orderInfoVo]
      * @Return: com.ruoyi.common.core.web.domain.AjaxResult
      * @Author: LeiMingkun
      * @Date: 2024/10/26 15:21
     *司机/用户取消订单
     */
    @Override
    public AjaxResult cancel(OrderInfoVo orderInfoVo) {
         //如果订单ID为空，进行返回
        if(orderInfoVo.getId()==null){
            return AjaxResult.error(OrderConstant.ORDER_ID_NOTEXISTS);
        }
         //查询订单是否存在，不存在返回错误信息
        OrderInfo orderInfo=cancelMapper.search(orderInfoVo.getId());
        if(Objects.isNull(orderInfo)){
            return AjaxResult.error(OrderConstant.ORDER_ID_MISTAKE);
        }
         //如果订单状态为9证明订单已被取消，返回错误信息
        if(orderInfo.getOrderStatus().equals(OrderConstant.ORDER_STATUS_NINE)){
            return AjaxResult.error(OrderConstant.ORDER_ID_DISABLED);
        }
         //如果订单状态为8证明订单支付完成无法取消订单，返回错误信息
        if(orderInfo.getOrderStatus().equals(OrderConstant.ORDER_STATUS_EIGHT)){
            return AjaxResult.error(OrderConstant.ORDER_ID_FINISH);
        }
         //如果订单状态为7证明订单发起收款无法取消订单，返回错误信息
        if(orderInfo.getOrderStatus().equals(OrderConstant.ORDER_STATUS_SEVEN)){
            return AjaxResult.error(OrderConstant.ORDER_ID_ARRIVED);
        }
        //如果订单状态为6证明订单到达目的地无法取消订单，返回错误信息
        if(orderInfo.getOrderStatus().equals(OrderConstant.ORDER_STATUS_SIX)){
            return AjaxResult.error(OrderConstant.ORDER_ID_ARRIVED);
        }
        //如果订单状态为5证明乘客上车无法取消订单，返回错误信息
        if(orderInfo.getOrderStatus().equals(OrderConstant.ORDER_STATUS_FIVE)){
            return AjaxResult.error(OrderConstant.ORDER_ID_RUNNING);
        }
        //如果订单状态为4证明司机到达目的地，如果取消订单需按照相应里程*每公里费用扣取行程费用
        if(orderInfo.getOrderStatus().equals(OrderConstant.ORDER_STATUS_FOUR)){
            if(orderInfoVo.getOperateId()==1){
                BigDecimal multiply = orderInfo.getExpectDistance().multiply(new BigDecimal(1.5));
                //里面需要调用微信接口，进行付款，然后返回付款信息是否支付成功，如果成功进行修改订单和优惠券表
                return null;
            }
            DriverCredit driverCredit=creditMapper.search(orderInfo.getDriverId());
            driverCredit.setCredit(driverCredit.getCredit()-2);
            creditMapper.update(driverCredit); 
            orderInfo.setCancelTime(new Date());
            orderInfo.setCancelOperator(2);
            orderInfo.setCancelTypeCode(2);
            orderInfo.setOrderStatus(9);
            cancelMapper.update(orderInfo);
            couponMapper.update(orderInfo);
            return AjaxResult.success(OrderConstant.RESULT_STATUS);
        }
        //如果司机处于未动身接乘客状态 则用户取消订单不扣钱司机取消扣一分
        if(orderInfoVo.getOperateId()==1){
            orderInfo.setCancelTime(new Date());
            orderInfo.setCancelOperator(1);
            orderInfo.setCancelTypeCode(1);
            orderInfo.setOrderStatus(9);
            cancelMapper.update(orderInfo);
            couponMapper.update(orderInfo);
            return AjaxResult.success(OrderConstant.RESULT_STATUS);
        }
        DriverCredit driverCredit=creditMapper.search(orderInfo.getDriverId());
        driverCredit.setCredit(driverCredit.getCredit()-1);
        creditMapper.update(driverCredit);
        orderInfo.setCancelTime(new Date());
        orderInfo.setCancelOperator(2);
        orderInfo.setCancelTypeCode(2);
        orderInfo.setOrderStatus(9);
        cancelMapper.update(orderInfo);
        couponMapper.update(orderInfo);
        return AjaxResult.success(OrderConstant.RESULT_STATUS);
    }
}
